package com.xls.commonlibrary.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.cooltechworks.views.ScratchTextView;
import com.orhanobut.logger.Logger;
import com.xls.commonlibrary.R;
import com.xls.commonlibrary.http.RxLifecycle;
import com.xls.commonlibrary.http.Transformer;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

// 刮刮乐弹框
public class ScratchDialog extends Dialog {

    AppCompatActivity context;
    ScratchTextView stFoo;

    // 刮了这些才关dialog
    double percentPass = 0.99;

    // 倒计时  秒
    private int delay = 15;
    // 开始刮才开始倒计时，不要多次开
    boolean isDelayed = false;

    public static ScratchDialog newInstance(AppCompatActivity context) {
        return new ScratchDialog(context);
    }

    private ScratchDialog(AppCompatActivity context) {
        super(context);
        this.context = context;
    }

    @Override
    public void show() {
        super.show();

    }

    @Override
    public void dismiss() {
        super.dismiss();
        isDelayed = false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        getWindow().setContentView(R.layout.dialog_scratch);
        initView();
    }

    private void initView() {
        stFoo = findViewById(R.id.st_foo);
        if (stFoo != null) {
            stFoo.setRevealListener(new ScratchTextView.IRevealListener() {
                @Override
                public void onRevealed(ScratchTextView scratchTextView) {
                    // 文字 都显示了 100%
                }

                @Override
                public void onRevealPercentChangedListener(ScratchTextView scratchTextView, float percent) {
                    //  刮去[文字部分]的百分比
                    // Toaster.show("百分比：" + percent);

                    delay();

                    if (percent > percentPass) {
                        stFoo.setText("恭喜!");
                        dismiss();
                    }

                }
            });


        }


    }


    void delay() {
        if (isDelayed) {
            return;
        }
        isDelayed = true;

        Observable.just(1)
                .delay(delay, TimeUnit.SECONDS)
                .compose(Transformer.transform())
                .compose(RxLifecycle.bindRxLifecycle(this.context))
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Logger.d("========ScratchDialog=========onSubscribe======");
                    }

                    @Override
                    public void onNext(@NonNull Integer integer) {

                        Logger.d("========ScratchDialog=========onNext======");
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Logger.d("========ScratchDialog=========onError======");
                    }

                    @Override
                    public void onComplete() {
                        Logger.d("========ScratchDialog=========onComplete======");
                        dismiss();
                    }
                });
    }

}
